<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="webjars/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div>
    <form th:action="@{/list}" style="display: inline;">
        <h1>核酸检测系统</h1>
        <select name="hospitalId">
            <option value="-1">----请选择---</option>
        </select>
        <input type="submit" value="查询"/>
    </form>
    <button onclick="javascript:location.href='toAdd'">添加</button>
</div>
<table>
    <tr>
        <td>被检测人</td>
        <td>被检测人手机号</td>
        <td>被检测人身份证号</td>
        <td>检测机构</td>
        <td>检测日期</td>
        <td>检测结果</td>
        <td>操作</td>
    </tr>
    <tr th:each="assay : ${assays}">
        <td th:text="${assay.assayUser}"></td>
        <td th:text="${assay.phone}"></td>
        <td th:text="${assay.cardNum}"></td>
        <td th:text="${assay.hospital.name}"></td>
        <td th:text="${#dates.format(assay.assayTime,'yyyy-MM-dd')}"></td>
        <td th:utext="${assay.assayResult==0?'检测中':'<font color=red>确诊</font>'}"></td>
        <td><a href="javascript:;" th:onclick="update(this,[[${assay.id}]])">确诊</a></td>
    </tr>
</table>
<script th:inline="javascript">
    let hospitalId=[[${hospitalId}]];
    $(function (){
        function update(obj,id){
            //alert(id)
            if(confirm('请确认该检测者是否确诊')){
                $.getJSON('/update/'+id+'/1',function (data){
                    if(data.flag){
                        $(obj).parent().prev().html('<font color="red">确诊</font>')
                    }
                })
            }
        }
        $("tr:even").attr('bgcolor',"#ccc")
        $("tr:first").attr('bgcolor',"gray")

        $.getJSON('hospitals',function (data){
            for (let i = 0; i <data.length ; i++) {
                console.log(data[i].id)
                if(data[i].id==hospitalId){
                    console.log(data[i].id)
                    $('select').append(' <option selected value='+data[i].id+'>'+data[i].name+'</option>')
                }else {
                    $('select').append(' <option value="'+data[i].id+'">'+data[i].name+'</option>')
                }
            }

        })
    })
</script>
</body>
</html>